package member.order.action;

import m.common.action.RequestMethod;
import m.common.model.util.ModelCheckUtil;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.system.exception.MException;
import m.system.util.DateUtil;
import m.system.util.JSONMessage;
import member.base.action.BaseAction;
import member.base.model.StoreInfo;
import member.base.model.UserInfo;
import member.base.util.PowerUtil;
import member.order.model.OrderModel;
import member.order.service.MonthWagesService;

import java.util.Date;

public abstract class OrderBaseAction<E extends OrderModel> extends BaseAction {

    protected void fillInitProperty(E model) throws Exception {
        model.setInputDate(new Date());
        model.setInputUser(PowerUtil.getUser());
        model.setServeUser(PowerUtil.getUser());
        model.setInputStore(PowerUtil.getStore());
        model.setStatus(OrderModel.StatusSave);
        model.setBusiness(PowerUtil.getBusiness());
    }
    protected void setMemberPhoneNumExpression(ModelQueryList queryList) throws Exception {
        boolean hasManage=PowerUtil.hasPower(UserInfo.PowerManage);
        String userOid=PowerUtil.getUser().getOid();
        if (!hasManage) {
            queryList.addFieldExpression("member.phoneNum",
            "if(#{member.inputUser.oid}='"+userOid+"',#{member.phoneNum},concat(left(#{member.phoneNum},3),'****',right(#{member.phoneNum},4)))");
        }
    }

    public abstract E getModel();
    public abstract E saveOrder() throws Exception;
    public abstract void deleteOrder(E model) throws Exception;
    public void submitAfter(E model) throws Exception{}
    public void backAfter(E model) throws Exception{}
    public abstract void confirmOrder() throws Exception;
    @RequestMethod(description = "保存",transaction = true)
    public JSONMessage save() throws Exception {
        E model=saveOrder();
        return JSONMessage.success("保存成功")
            .push("oid",model.getOid());
    }
    @RequestMethod(description = "删除",transaction = true)
    public JSONMessage delete() throws Exception {
        deleteOrder(getModel());
        return JSONMessage.success("删除成功");
    }
    @RequestMethod(description = "保存提交",transaction = true)
    public JSONMessage submit() throws Exception {
        E model=saveOrder();
        ModelCheckUtil.equals(model,new String[]{"status","inputUser.oid"},new Object[]{OrderModel.StatusSave,PowerUtil.getUser().getOid()},"状态错误");
        PowerUtil.verifyAccountDate(model.getAccountDate());
        if(getService(MonthWagesService.class).isConfirmed(model.getServeUser().getOid(),model.getAccountDate())){
            throw new MException(model.getServeUser().getRealName()+DateUtil.format(model.getAccountDate(),"yyyy年M月")+"工资已发放，请更换账期。");
        }
        model.setStatus(OrderModel.StatusConfirm);
        ModelUpdateUtil.updateModel(model,"status");
        submitAfter(model);
        if(StoreInfo.StoreCheckYes.equals(PowerUtil.getStore().getStoreCheck())) {
            return JSONMessage.success("提交成功")
                .push("oid", model.getOid());
        }else{
            return confirm();
        }
    }
    @RequestMethod(description = "退回",transaction = true)
    public JSONMessage back() throws Exception {
        PowerUtil.verifyPower(UserInfo.PowerStore);
        E model=getModel();
        ModelCheckUtil.equals(model,new String[]{"status","inputStore.oid"},new Object[]{OrderModel.StatusConfirm,PowerUtil.getStore().getOid()},"状态错误");
        model.setStatus(OrderModel.StatusSave);
        ModelUpdateUtil.updateModel(model,"status");
        backAfter(model);
        return JSONMessage.success("退回成功");
    }
    @RequestMethod(description = "确认",transaction = true)
    public JSONMessage confirm() throws Exception {
        E model=getModel();
        ModelCheckUtil.equals(model,new String[]{"status","inputStore.oid"},new Object[]{OrderModel.StatusConfirm,PowerUtil.getStore().getOid()},"状态错误");
        confirmOrder();
        return JSONMessage.success("确认成功");
    }
}
